﻿/*  ----------------------------------------------------------------------------
 *  SysServer
 *  ----------------------------------------------------------------------------
 *  File:       AllocatedSegment.cs
 *  Creater:     SEEN\MistSeen
 *  ----------------------------------------------------------------------------
 *  $Date: 2010-09-06 10:36:01 +0000 (Mon, 06 Sep 2010) $
 *  $Rev: 15 $
 *  $Author: ScorDeity $
 *  $URL: http://sysserver.googlecode.com/svn/trunk/src/SysServer/Collections/AllocatedSegment.cs $
 *  $Id: AllocatedSegment.cs 15 2010-09-06 10:36:01Z ScorDeity $
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace SysServer.Collections
{
    public class AllocatedSegment : DisposableEntity
    {
        public byte[] MemorySegment;
        private GCHandle _SegmentHandle;

        public unsafe byte* SegmentPointer
        {
            get
            {
                if (IsDisposed)
                    throw new ObjectDisposedException("allocated segment");

                return (byte*)this._SegmentHandle.AddrOfPinnedObject().ToPointer();
            }
        }
        public AllocatedSegment(byte[] memorySegment)
        {
            this.MemorySegment = memorySegment;
            this._SegmentHandle = GCHandle.Alloc(memorySegment, GCHandleType.Pinned);
        }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            this._SegmentHandle.Free();
            if (disposing)
            {
                this.MemorySegment = new byte[0];
            }
        }

    }
}